package com.youlu.campus.web.community.controller;


import com.youlu.campus.entity.VO.req.QaCommitCommand;
import com.youlu.campus.entity.VO.req.QaInfoReq;
import com.youlu.campus.entity.VO.req.QaPracticeCommitCommand;
import com.youlu.campus.entity.VO.res.H5QaPracticeErrorQuery;
import com.youlu.campus.service.activity.ActivityH5PlusService;
import com.youlu.campus.service.qa.ActivityQaInfoService;
import com.youlu.campus.service.qa.UserQaInfoService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * H5新活动控制器
 */
@Slf4j
@RestController
public class ActivityH5PlusController {

    @Autowired
    private ActivityH5PlusService activityH5PlusService;

    @Autowired
    private UserQaInfoService userQaInfoService;

    @Autowired
    private ActivityQaInfoService activityQaInfoService;

    /**
     * 获取活动详情
     *
     * @param activityId
     * @return
     */
    @GetMapping("/public/h5-plus/activity-detail")
    public QResult activityDetail(@RequestParam("activityId") String activityId) {
        return QResult.success(activityH5PlusService.activityDetailByActivityId(activityId));
    }

    /**
     * 获取用户是否参与该活动
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/h5-plus/check-join")
    public QResult getUserJoinedActivity(@RequestParam("activityId") String activityId) {
        return QResult.success(activityH5PlusService.checkUserJoinActivity(RequestContext.getCurUser(),activityId));
    }

    /**
     * 获取活动详情进度页
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/h5-plus/schedule/activity-detail")
    public QResult findActivityDetailSchedule(@RequestParam("activityId") String activityId) {
        return QResult.success(activityH5PlusService.findActivityDetailSchedule(RequestContext.getCurUser(),activityId));
    }

    /**
     * 获取用户参加习题结果
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/h5-plus/user/qa-practice")
    public QResult findUserQaPractice(@RequestParam("activityId") String activityId,
                                      @RequestParam(value = "qaInfoId",required = false) String qaInfoId) {
        return QResult.success(userQaInfoService.findUserQaQaPracticeByUserId(RequestContext.getCurUser(),activityId,qaInfoId));
    }


    /**
     * 习题答题提交-新版H5
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/h5-plus/user-qa-practice/commit")
    public QResult qaCommit(@RequestBody @Validated QaPracticeCommitCommand qaPracticeCommitCommand,
                            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        qaPracticeCommitCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userQaInfoService.doQaPracticeCommit(qaPracticeCommitCommand));
    }

    /**
     * 查询获取答题信息-习题
     *
     * @param qaInfoReq
     * @return
     */
    @PostMapping("/mini-app/h5-plus/qa-practice/query")
    public QResult qaInfoPractice(@RequestBody @Validated QaInfoReq qaInfoReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        qaInfoReq.setCurrentUser(RequestContext.getCurUser());
        qaInfoReq.setQaType("2");
        return QResult.success(activityQaInfoService.getQaInfo(qaInfoReq));
    }

    /**
     * 查询获取答题信息-习题
     *
     * @param qaInfoReq
     * @return
     */
    @PostMapping("/mini-app/h5-plus/qa-practice/reset")
    public QResult qaInfoPracticeRest(@RequestBody @Validated QaInfoReq qaInfoReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        qaInfoReq.setCurrentUser(RequestContext.getCurUser());
        userQaInfoService.qaPracticeReset(qaInfoReq);
        return QResult.success(null);
    }

    /**
     * 获取用户错误题库练习题库列表
     *
     * @param h5QaPracticeErrorQuery
     * @return
     */
    @PostMapping("/mini-app/user/qa-error/list")
    public QResult findUserQaPracticeErrorList(@RequestBody @Validated H5QaPracticeErrorQuery h5QaPracticeErrorQuery,
                                               BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        h5QaPracticeErrorQuery.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userQaInfoService.findUserQaPracticeErrorList(h5QaPracticeErrorQuery));
    }

    /**
     * 获取用户答题信息
     *
     * @param h5QaPracticeErrorQuery
     * @return
     */
    @PostMapping("/mini-app/user/qa-log/list")
    public QResult findUserQaPracticeLogList(@RequestBody @Validated H5QaPracticeErrorQuery h5QaPracticeErrorQuery,
                                               BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        h5QaPracticeErrorQuery.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userQaInfoService.findUserQaLog(h5QaPracticeErrorQuery));
    }




    /**
     * 答题提交-新版H5
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/h5-plus/user-qa/commit")
    public QResult qaCommit(@RequestBody @Validated QaCommitCommand commitCommand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        commitCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userQaInfoService.doH5PlusQaCommit(commitCommand));
    }

    /**
     * 查询获取答题信息
     * @param qaInfoReq
     * @return
     */
    @PostMapping("/mini-app/h5-plus/user-qa/query")
    public QResult qaInfo(@RequestBody @Validated QaInfoReq qaInfoReq, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        qaInfoReq.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(activityQaInfoService.getH5PlusQaInfo(qaInfoReq));
    }

    /**
     * 答题提交-每日挑战
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/h5-task/user-qa/commit")
    public QResult qaTaskCommit(@RequestBody @Validated QaCommitCommand commitCommand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        commitCommand.setCurrentUser(RequestContext.getCurUser());
        commitCommand.setType(6);
        return QResult.success(userQaInfoService.doQaCommit(commitCommand));
    }

    /**
     * 获取用户是否参与该活动
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/h5/check/sign-up")
    public QResult getH5UserJoinedActivity(@RequestParam("activityId") String activityId) {
        return QResult.success(activityH5PlusService.checkH5UserJoinActivity(RequestContext.getCurUser(),activityId));
    }

}
